<?php

namespace app\admin\controller;

use app\admin\controller\common\Base;

class Role extends Base
{
    public function index()
    {
        return $this->fetch();
    }
    public function list()
    {
        $role = db('role')->field('id,name,sort,power')->where(['is_del' => 0])->order('sort desc')->select();

        return ['code' => 0, 'data' => $role, 'count' => count($role), 'msg' => ''];
    }
    public function add()
    {
        return $this->fetch();
    }

    public function powerlist()
    {

        //查询顶级列表
        $menuinfo = db('menuinfo')->field('id,title,is_menu,sort,pid')->where(['is_menu' => 1, 'pid' => -1, 'is_del' => 0])->order('sort desc')->select();
        if (!empty($menuinfo)) {
            foreach ($menuinfo as $k => $v) {
                //查询一级
                $children = db('menuinfo')->field('id,title,is_menu,sort,pid')->where(['is_menu' => 1, 'pid' => $v['id'], 'is_del' => 0])->order('sort desc')->select();
                if ($children) {
                    $v['children'] =  $children;
                    $v['spread'] =  true;
                    foreach ($v['children'] as $k2 => $v2) {
                        //查询二级
                        $children2 = db('menuinfo')->field('id,title,is_menu,sort,pid')->where(['is_menu' => 1, 'pid' => $v2['id'], 'is_del' => 0])->order('sort desc')->select();
                        if ($children2) {
                            $v2['children'] = $children2;
                            $v2['spread'] =  true;
                            //查询权限
                            foreach ($v2['children'] as $k3 => $v3) {
                                $children3 = db('menuinfo')->field('id,title,is_menu,sort,pid')->where(['is_menu' => 0, 'pid' => $v3['id'], 'is_del' => 0])->order('sort desc')->select();
                                if ($children3) {
                                    $v3['children'] = $children3;
                                    $v3['spread'] =  true;
                                }
                                $v2['children'][$k3] = $v3;
                            }
                        }
                        $v['children'][$k2] = $v2;
                    }
                }
                $menuinfo[$k] = $v;
            }
        }
        $data['data'] = $menuinfo;
        $data['msg'] = '';
        $data['count'] = count($menuinfo);
        return $data;
    }
    public function addvalue()
    { //新增身份
        $val = input('post.');
        $data['power'] = $val['power'];
        $data['name'] = $val['name'];
        $data['sort'] = $val['sort'];
        $data['inTime'] = $data['upTime'] = time();
        $res = db('role')->insert($data);
        return ['code' => $res];
    }

    public function find()
    { //查询数据
        $id = input('post.id');
        $res = db('role')->where(['id' => $id])->find();
        if ($res) {
            $res['power'] = explode(",", $res['power']);
            //为适用layui.tree
            $res['power'] = $this->powerse($res['power']);
            return ['code' => 1, 'data' => $res];
        } else {
            return ['code' => 0];
        }
    }
    private function powerse($power,$times = 0){
        if($times < 3){
            $powers = db('menuinfo')->where(['id' => ['in', $power], 'pid' => ['in', $power], 'is_del' => 0])->column('id');
            $times ++;
            return $this->powerse($powers,$times);
        }else{
            return $power;
        }
       

    }
    //修改数据
    public function edit()
    {
        $val = input('post.');
        $data['id'] = $val['id'];
        $data['power'] = $val['power'];
        $data['name'] = $val['name'];
        $data['sort'] = $val['sort'];
        $data['upTime'] = time();
        $res = db('role')->update($data);
        return ['code' => $res];
    }
    //删除
    public function del()
    {
        $id = input('post.id');
        $res =  db('role')->where(['id' => $id])->update(['is_del' => 1, 'upTime' => time()]);
        return ['code' => $res];
    }
}
